<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Node;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //搜索关键字
        $keyword = $request->get('keyword');

        $data = Role::when($keyword,function ($query) use ($keyword){
            $query->where('rolename','like','%'.$keyword.'%');
        })->paginate($this->paginate);

        return view('admin.role.index',compact('data','keyword'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * 执行添加
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $parmas = $request->except('_token');
        $data = $request->validate([
            'rolename' => 'required|unique:roles|min:3|max:9',

        ],[
            'rolename.required'=>'角色不能为空',
            'rolename.unique'=>'角色已经存在',
            'rolename.min'=>'角色名称必须大于三个字符',
            'rolename.max'=>'角色名称必须小于九个字符',
        ]);
        //send(1,2) 必须有页面 参数1：页面的地址 参数2：发送的内容
//        \Mail::send('admin.user.email',$params,function (\Illuminate\Mail\Message $message ){
//            $message->to('18569820614@139.com');
//            $message->subject('测试');
//        });


        $res = Role::create($data);

        return redirect(route('admin.role.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 修改页面
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Role $role)
    {
        //
        return view('admin.role.edit',compact('role'));
    }

    /**
     * 执行修改
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,Role $role)
    {
        //
        $request->validate([
            'rolename' => 'required|unique:roles|min:3|max:9',
        ],[
            'rolename.required'=>'角色不能为空',
            'rolename.unique'=>'角色已经存在',
            'rolename.min'=>'角色名称必须大于三个字符',
            'rolename.max'=>'角色名称必须小于九个字符',
        ]);

        $role->rolename = $request->get('rolename');

        $res = $role->save();

        if($res){
            return redirect(route('admin.role.index'));
        }else{
            return redirect(route('admin.role.edit',compact('role')))->withErrors('修改失败')   ;
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = Role::destroy($id);

        if($res){
            return $this->witeJson(0,[],'删除角色成功');
        }else{
            return  $this->witeJson(20001,[],'删除角色失败');
        }

    }

    public function node(Role $role,Request $request)
    {
        if($request->isMethod('post')){
            $params = $request->except('_token');

            //toggle()  会删除原来拥有的数据
            $res = $role->nodes()->sync($params['ids']);

            if($res){
                return redirect(route('admin.role.node',$role));
            }else{
                return redirect(route('admin.role.node',$role))->withErrors('添加权限失败');
            }
        }

        //查找所有权限
        $data  = (new Node())->getnodeAll();

        //pluck 方法返回指定键的所有值。
        $rolenode = $role->nodes()->pluck('node_id')->toArray();
        return view('admin.role.node',compact('role','data','rolenode'));
    }
}
